package com.tg.app.util;

import android.content.Context;

import com.tange.core.backend.service.api.CoreApiUrl;
import com.tange.core.backend.service.http.ClientObserver;
import com.tange.core.backend.service.http.CoreHttpClient;
import com.tange.base.toolkit.GlobalApplicationContext;
import com.tange.base.toolkit.StringUtils;
import com.tg.appcommon.android.TGLog;
import com.tg.network.socket.http.TanGeServiceApi;
import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;
import java.util.Properties;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Describe: 日志管理器
 * Created by Rance Rang on 2019/3/14.
 * <p>
 * Copyright (c) Tange.Inc All rights reserved.
 */
public class LogUtils {

    public static void onEventClickByName(String name,String btn){
        if (GlobalApplicationContext.application() == null) return;
        MobclickAgent.onEvent(GlobalApplicationContext.application(),"TG_Event_Click_"+name,btn);
    }
    public static  String getBaiduEventID(String eventId){
        if (eventId.length()>30){
            return  eventId.substring(0,30);
        }
        return eventId;
    }
    public static void matTrackCustomKVEvent(Context context, String event, String key) {
        Properties prop = new Properties();
        prop.setProperty("name", key);
    }

    public static void trackCustomBeginKVEvent(Context context, String event, String key) {
        Properties prop = new Properties();
        prop.setProperty("name", key);
    }

    public static void trackCustomEndKVEvent(Context context, String event, String key) {
        Properties prop = new Properties();
        prop.setProperty("name", key);
    }

    public static void uploadLogE(String title, String content) {
        HashMap<String, String> map = new HashMap<>();
        map.put("title", title);
        map.put("detail", content);
        map.put("level", "FATAL");
        uploadLog(map);
    }

    public static void uploadLogN(String title, String content) {
        HashMap<String, String> map = new HashMap<>();
        map.put("title", title);
        map.put("detail", content);
        map.put("level", "NORMAL");
        uploadLog(map);
    }

    public static void uploadLog(HashMap<String, String> map) {
        if (!StringUtils.isEmpty(CoreApiUrl.TG_UPLOAD_LOG)){
            map.put("source", "android");
            map.put("function_module", "APP端");
            map.put("path", "Camera/command");

            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(CoreApiUrl.TG_UPLOAD_LOG)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .client(CoreHttpClient.getOkHttpClient())
                    .build();
            TanGeServiceApi api = retrofit.create(TanGeServiceApi.class);
            api.uploadExceptionReport(map)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new ClientObserver<String>() {
                        @Override
                        protected void onSuccess(String content) {
                            TGLog.d("upload = " + content);
                        }
                    });
        }

    }

}
